package com.yuandian.management.controller;

import cn.hutool.core.exceptions.UtilException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.management.entity.ConfFilterBusinessData;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.log.annotation.SysLog;
import com.yuandian.management.service.ConfFilterBusinessDataService;
import com.yuandian.management.utils.JacksonUtil;
import com.yuandian.management.vo.FilterBusinessDataBodyUpload;
import com.yuandian.utils.poi.WordUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 原始数据样包存储表-Controller
 * @author zfsn
 * @date 2023-4-13
 */
@Api(tags = "原始数据样包分析-原始数据包展示")
@RestController
@RequestMapping("/filterBusinessData")
public class ConfFilterBusinessDataController {

	@Autowired
	private ConfFilterBusinessDataService confFilterBusinessDataService;

	/**
	 * 通过主键查询数据
	 * @param idList 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键查询数据", notes = "通过主键查询数据，支持批量查询")
	@GetMapping("/{ids}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "id(支持多选)", paramType = "query", required = true, dataTypeClass = Integer.class)
	})
	public R<List<ConfFilterBusinessData>> queryByIds(@PathVariable("ids") List<Integer> idList) {
		return R.ok(confFilterBusinessDataService.listByIds(idList));
	}

	/**
	 * 分页查询
	 * @param confFilterBusinessData 筛选条件
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@PostMapping
	public R<Page<ConfFilterBusinessData>> listPage(@RequestBody ConfFilterBusinessData confFilterBusinessData) {
		return R.ok(confFilterBusinessDataService.listPage(confFilterBusinessData));
	}

	/**
	 * 新增数据
	 * @param confFilterBusinessData 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "新增数据", notes = "新增数据")
	@SysLog("新增专属指标表数据")
	@PostMapping("/save")
	public R<Boolean> save(@RequestBody ConfFilterBusinessData confFilterBusinessData) {
		return R.ok(confFilterBusinessDataService.save(confFilterBusinessData));
	}

	/**
	 * 通过主键更新数据
	 * @param confFilterBusinessData 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键更新数据", notes = "通过主键更新数据")
	@SysLog("更新专属指标表数据")
	@PutMapping
	public R<Boolean> updateById(@RequestBody ConfFilterBusinessData confFilterBusinessData) {
		return R.ok(confFilterBusinessDataService.updateById(confFilterBusinessData));
	}

	/**
	 * 通过主键删除数据
	 * @param ids 主键
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键删除数据", notes = "通过主键删除数据")
	@SysLog("删除专属指标表数据")
	@DeleteMapping("/delete/{ids}")
	public R<Boolean> removeByIds(@PathVariable("ids") List<Integer> ids){
		return R.ok(confFilterBusinessDataService.removeByIds(ids));
	}

	/**
	 * 打包下载成多个文件.封装到压缩包中
	 */
	@GetMapping("/excelTemplate-all/{ids}")
	public void exportBaseAlarmExcel(@PathVariable("ids") List<Integer> ids, HttpServletRequest request, HttpServletResponse response) {
		String filePath = System.getProperty("java.io.tmpdir");
		try {
			List<File> listFile = new ArrayList<>();
			List<ConfFilterBusinessData> list = confFilterBusinessDataService.listByIds(ids);
			for (ConfFilterBusinessData dto : list) {
				FilterBusinessDataBodyUpload upload = confFilterBusinessDataService.updateUploadContent(dto);
				String fileName = upload.getBussType() + "+" + upload.getSessionId();
				listFile.add(WordUtil.ecportFileData2(filePath, upload.toString(), fileName));
			}
			WordUtil.downLoadFiles(listFile, request, response);
			// 调用导表模板工具类
			if (!listFile.isEmpty()) {
				for (File file : listFile) {
					if (file.exists()) {
						//如果存在则删除
						file.delete();
					}
				}
			}
		} catch (Exception e) {
			throw new UtilException("样包下载异常");
		}
	}

	/**
	 * 打包下载成多个文件.封装到压缩包中
	 */
	@GetMapping("/excelTemplate-one/{id}")
	public void excelTemplateOne(@PathVariable("id") Integer id, HttpServletResponse response) {
		try {
			ConfFilterBusinessData dto = confFilterBusinessDataService.getById(id);
			if(dto==null) {
				response.reset();
				response.setContentType("application/json");
				response.setCharacterEncoding("utf-8");
				try {
					response.getWriter().println(JacksonUtil.objectToString(R.failed()));
				} catch (IOException e2) {
					e2.printStackTrace();
				}
			} else {
				FilterBusinessDataBodyUpload upload = confFilterBusinessDataService.updateUploadContent(dto);
				String fileName = upload.getBussType() + "+" + upload.getSessionId();
				// 调用导表模板工具类
				WordUtil.ecportFileData(response, upload.toString(), fileName);
			}
		} catch (Exception e) {
			throw new UtilException("样包下载异常");
		}
	}



}
